package com.linghu.view;

import com.linghu.model.House;
import com.linghu.service.HouseService;
import com.sun.xml.internal.ws.addressing.WsaActionUtil;

import java.util.Scanner;

public class HouseView {
    //菜单循环变量loop
    boolean loop = true;
    Scanner scanner=new Scanner(System.in);//输入
    String key;//选择变量
    String choice="";//退出变量
    private HouseService houseService=new HouseService(10);//10个房屋信息


    //update界面，修改界面
    public void updateHouse(){

        System.out.println("请选择（1-6）");
        System.out.println("========修改用户==========");
        System.out.println("请选择修改房屋的编号（-1退出）");
        int updateId=scanner.nextInt();
        if (updateId==-1){
            System.out.println("你放弃修改房屋");
            return;
        }
        House houses=houseService.findById(updateId);
        if (houses!=null){
            System.out.print("姓名("+houses.getName()+"):");
            String name=scanner.next();
            if (!"".equals(name)){
                houses.setName(name);
            }

            System.out.print("电话("+ houses.getPhone()+"):");
            String phone=scanner.next();
            if (!"".equals(phone)){
                houses.setPhone(phone);
            }

            System.out.print("地址（"+houses.getAddress()+"）:");
            String address=scanner.next();
            if (!"".equals(address)){
                houses.setAddress(address);
            }

            System.out.println("租金（"+houses.getRent()+"）：");
            int rent=scanner.nextInt();
            if (!"".equals(rent)){
                houses.setRent(rent);
            }

            System.out.println("状态（"+houses.getState()+"）：");
            String state=scanner.next();
            if (!"".equals(state)){
                houses.setState(state);
            }

        }
        System.out.println("修改房屋信息完成！");

    }

    //findHouse界面
    public void findHouse(){
        System.out.println("---------查找房屋----------");
        System.out.println("请输入你要查找的id");
        int findId=scanner.nextInt();
        House houses=houseService.findById(findId);
        if (houses!=null){
            System.out.println(houses);
        }else {
            System.out.println("没有该房屋id！");
        }
    }


    //编写一个delHouse界面
    public void delHouse(){
        System.out.println("=====删除房屋====");
        System.out.println("请选择删除房屋编号(-1退出)：");
        int delId=scanner.nextInt();
        while (true){
            System.out.println("是否确认删除y/n");
            String choice=scanner.next();
            if ("y".equals(choice)){
                if (houseService.del(delId)){
                    System.out.println("删除成功！");
                    break;
                }else{
                    System.out.println("删除失败");
                    break;
                }
            }else {
                System.out.println("删除失败");
                break;
            }
        }

    }

    //addHouse界面
    public void addHouse(){
        System.out.println("======添加房屋=========");
        System.out.print("姓名：");
        String name=scanner.next();
        System.out.print("电话：");
        String phone=scanner.next();
        System.out.print("地址：");
        String address=scanner.next();
        System.out.print("月租：");
        int rent=scanner.nextInt();
        System.out.print("状态：");
        String state=scanner.next();
        House house = new House(0, name, phone, address, rent, state);
        if (houseService.add(house)){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }


    //房屋列表listHouses
    public void listHouses(){
        System.out.println("==========房屋列表===========");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态（已出租，未出租）");
        House house[]=houseService.list();
        for (int i = 0;i < house.length;i++){
            if (house[i]==null){
                break;
            }
            System.out.println(house[i]);
        }
        System.out.println("==========房屋列表展示完成===========");
    }
    public void mainMenu(){

        do {
            System.out.println("=========房屋出租系统=============");
            System.out.println("\t\t\t1、新增房源");
            System.out.println("\t\t\t2、查找房源");
            System.out.println("\t\t\t3、删除房源");
            System.out.println("\t\t\t4、修改房源信息");
            System.out.println("\t\t\t5、房屋列表");
            System.out.println("\t\t\t6、退   出");
            System.out.println("请做出选择(1-6)");
            key=scanner.next();//输入自己的选择
            switch (key){
                case "1":
                    addHouse();
                    break;
                case "2":
                    findHouse();
                    break;
                case "3":
                    delHouse();
                    break;
                case "4":
                    updateHouse();
                    break;
                case "5":
                    listHouses();
                    break;
                case "6":
                        while (true){
                            System.out.println("你确定要退出吗？y/n");
                            choice=scanner.next();
                            if ("y".equals(choice)){
                                loop=false;//循环菜单变量loop
                                break;
                            }else {
                                loop=true;
                                break;
                            }
                        }
                default:break;
            }
        }while (loop);
        System.out.println("已确认退出该系统。。。");
    }
}
